package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.CommentMapper;
import cn.tedu.weibo.pojo.dto.CommentDTO;
import cn.tedu.weibo.pojo.entity.Comment;
import cn.tedu.weibo.pojo.vo.CommentVo;
import cn.tedu.weibo.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class CommentController {
    @Autowired(required = false)
    private CommentMapper commentMapper;

    @RequestMapping("/comment/insert")
    public int insert(@RequestBody CommentDTO commentDTO, HttpSession session){
        UserVO user = (UserVO) session.getAttribute("user");
        System.out.println("user:" + user);
        if (user==null){
            return 2;
        }
        System.out.println("commentDTO:" + commentDTO);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(user.getId());
        System.out.println("comment:"+comment);

        commentMapper.insertOne(comment);

        return 1;
    }
    @RequestMapping("/comment/selectAllByWeiboId")
    public List<CommentVo> selectComment(int id){
        List<CommentVo> commentVos = commentMapper.selectAllByWeiboId(id);
        System.out.println("commentVos==>"+commentVos);
        return commentVos;
    }
}
